package edu.wschina.module_c_hanoitower;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import java.util.List;
import java.util.Stack;

public class TowersView extends View {

    //塔柱的圆盘;
    private List<Stack<Integer>> towers;
    private int diskVGap = 1;
    private int[] diskWidths = {500, 400, 300, 200};
    private int[] colors = {Color.GREEN, Color.RED, Color.BLUE, Color.DKGRAY};
    private int diskHeight = 80; // 圆盘高度
    //baseY是第一个塔底部的Y绝对坐标
    int baseY = 1360;
    //baseX是第一个塔底部中心X坐标
    int baseX = 300;

    public TowersView(Context context, List<Stack<Integer>> towers) {
        super(context);
        this.towers = towers;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        int x = baseX;


        //绘制塔柱
        int pillarHeight = 800; // 塔柱高度
        int pillarWidth = 40; // 塔柱粗细
        // 塔柱间距
        int diskHGap = getWidth() / MyApp.getInstance().diskCount;
        paint.setColor(Color.BLACK); // 塔柱颜色
        for (int i = 0; i < MyApp.getInstance().diskCount; i++) {
            canvas.drawRect(
                    baseX + i * diskHGap - pillarWidth / 2, // 塔柱左边界
                    baseY - pillarHeight, // 塔柱顶部y坐标
                    baseX + i * diskHGap + pillarWidth / 2, // 塔柱右边界
                    baseY, // 塔柱底部y坐标
                    paint
            );
        }

        //绘制底座
        int baseWidth = 585; // 底座长度
        int baseHeight = 40; // 底座高度
        paint.setColor(Color.BLACK); // 底座颜色
        for (int i = 0; i < MyApp.getInstance().diskCount; i++) {
            canvas.drawRect(
                    baseX + i * diskHGap - baseWidth / 2, // 底座的左边界
                    baseY, // 底座的顶部y坐标
                    baseX + i * diskHGap + baseWidth / 2, // 底座的右边界
                    baseY + baseHeight, // 底座的底部y坐标
                    paint
            );
        }


        /*@description:
        绘制3个塔，以及塔中的圆盘
        @author aimin 2025-01-08 12:36:24*/
        for (int i = 0; i < this.towers.size(); i++) {
            int y = baseY;
            /*@description:
            绘制单个塔中的圆盘
            @author aimin 2025-01-08 12:36:43*/
            Stack<Integer> tower = towers.get(i);
            for (int j = 0; j < tower.size(); j++) {
                int m = tower.get(j);
                paint = new Paint();
                paint.setColor(colors[m]);
                int width = diskWidths[m];

                canvas.drawRoundRect(x - width / 2 + i * diskHGap,
                        y - diskHeight,
                        x + width / 2 + i * diskHGap,
                        y,
                        20, 20,
                        paint);
                y = y - diskHeight - diskVGap;
            }
        }
    }
}

// if (draggingDisk != -1) {
//            drawDraggingDisk(canvas);
//        }


//private void drawDraggingDisk(Canvas canvas) {
//    int diskWidth = draggingDisk * 40;
//    paint.setColor(Color.RED);
//    canvas.drawRect(
//            dragX - diskWidth / 2,
//            dragY - 20,
//            dragX + diskWidth / 2,
//            dragY,
//            paint
//    );